import 'package:core_package/ui/alert/show_bottom_alert_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/model/channel_model.dart';
import 'package:flutter_app_unionad/request/workbench_request.dart';
import 'package:core_package/core_package.dart';

class WorkSelectChannelWidget extends StatefulWidget {
  @override
  _WorkSelectChannelWidgetState createState() => _WorkSelectChannelWidgetState();

  static Future show(BuildContext context) async {
    return ShowBottomAlertDialog.showBottomDialog(context, builder: (ctx) {
      return WorkSelectChannelWidget();
    });
  }
}

class _WorkSelectChannelWidgetState extends State<WorkSelectChannelWidget> {
  List<ChannelModel> ?list;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WorkBenchRequest.getChannelList().then((value) {
      if (mounted)
        setState(() {
          list = value;
        });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 15.px),
      child: Column(
        children: [
          MySizeBox(height: 10),
          ShowTitleDetailListWidget(
            title: '选择渠道',
            lineColor: MyColors.color_151_151_151,

            padding: EdgeInsets.zero,
            linePadding: EdgeInsets.zero,
            icon: IconButton(
              onPressed: () => Navigator.pop(context),
              icon: Icon(
                Icons.close,
                size: 24,
                color: MyColors.color_34_34_34,
              ),
            ),
          ),
          Expanded(child: buildContent()),
        ],
      ),
    );
  }

  Widget buildContent() {
    if (list == null) return ProgressDialog.showFirstLoading();
    if (list!.length == 0) return EmptyWidget();
    return ListView.builder(
      itemCount: list!.length,
      itemBuilder: (context, index) {
        return ShowTitleDetailListWidget(
          showBottomLine: false,
          padding: EdgeInsets.symmetric(horizontal: 5.px, vertical: 5.px),
          linePadding: EdgeInsets.symmetric(horizontal: 5.px, vertical: 5.px),
          title: list![index].channelName,
          onTap: () => Navigator.pop(context, list![index]),
        );
      },
      padding: EdgeInsets.zero,
    );
  }
}
